
package com.hd.sup.modules.vehicle.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.DateUtil;
import com.hd.sup.modules.vehicle.model.VehicleTrail;
import com.hd.sup.modules.vehicle.service.IVehicleTrailService;
import com.hd.sup.modules.vehicle.wrapper.VehicleTrailWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title VehicleDrivingController.java
 * @Description 车辆行驶轨迹控制器
 * @Package com.hd.sup.modules.vehicle.controller
 * @date 2020/12/31 11:37 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/vehicleTrail")
public class VehicleTrailController extends BaseController {


    @Resource
    private IVehicleTrailService vehicleTrailService;

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查询车辆行驶轨迹gps点集合
     * @date 2020/12/31 11:37 上午
     * @params [requestData]
     */
    @PostMapping("/track")
    public Result<?> vehicleDrivingList(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String platNo = requestData.getString("platNo");
        String platNoColor = requestData.getString("platNoColor");
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");

        if (StringUtils.isAnyEmpty(platNo, platNoColor, startDateStr, endDateStr))
            return Result.error("参数中 platNo 或 platNoColor 或 startDateStr 或 endDateStr为空");

        if (current == null || size == null) {

            VehicleTrail param = new VehicleTrail(platNo, platNoColor, startDateStr, endDateStr);
            //入参校验
            List<Map<String, Object>> track = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleTrailService.getVehicleTrack(param)).warp();

            return Result.success(track);

        } else {

            Page<Map<String, Object>> page = new Page<>();
            page.setCurrent(current);
            page.setSize(size);
            VehicleTrail param = new VehicleTrail(platNo, platNoColor, startDateStr, endDateStr);
            //入参校验
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleTrailService.getVehicleTrackPage(page, param)).warp();
            page.setRecords(result);
            return Result.success(page);
        }


    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查询实时轨迹
     * @date 2021/2/3 2:34 下午
     * @params [requestData]
     */
    @PostMapping("/realTrack")
    public Result<?> realTrack(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String platNo = requestData.getString("platNo");
        String platNoColor = requestData.getString("platNoColor");

        if (StringUtils.isAnyEmpty(platNo, platNoColor))
            return Result.error("参数中 platNo 或 platNoColor为空");

        //取车辆上次点火时间
        VehicleTrail param = new VehicleTrail(platNo, platNoColor);
        param.setVehicleStatus(1L);
        String accOnGpsTime = vehicleTrailService.getLastAccGpsTime(param);
        //取车辆上次熄火时间
        param.setVehicleStatus(0L);
        String accOffGpsTime = vehicleTrailService.getLastAccGpsTime(param);
        param.setStartDateStr(accOffGpsTime);
        param.setEndDateStr(accOnGpsTime);
        if (current == null || size == null) {
            //入参校验
            List<Map<String, Object>> track = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleTrailService.getVehicleRealTrack(param)).warp();

            return Result.success(track);
        } else {
            Page<Map<String, Object>> page = new Page<>();
            page.setCurrent(current);
            page.setSize(size);
            //入参校验
            List<Map<String, Object>> track = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleTrailService.getVehicleRealTrackPage(page, param)).warp();
            page.setRecords(track);
            return Result.success(page);
        }
    }

}
